<?php
// @brief: show html and movie list
class XPlayList 
{ 
	/*{{{*/
	private $_html_dir = '/media/sd/htmls';
	private $_movie_dir = '/media/sd/movies';
	//private $_html_dir = 'c:\\htmls\\';
	//private $_movie_dir = 'c:\\movies\\';
	//private $_html_dir = '/home/liheefan/htmls';
	//private $_movie_dir = '/home/liheefan/movies';
	//private $_routine_path = '/home/liheefan/playlist';
	private $_routine_path = '/usr/infomat/playlist';
	private $_routine_bak_path = '/usr/infomat/playlist_bak';
	private $_mode ; // -1 insert mode, 
	private $_html_files = array();
	private $_movie_files = array();
	private $_routine_ini = array();
	/*}}}*/

	/*{{{*/  //For filemanage.php
	function __construct()  /*{{{*/
	{
		$this->getHtmlList();
	} /*}}}*/

	/* ****************************************
	 *  The list from /media/sd/htmls/
	 * ****************************************/
	public function getHtmlList()  /*{{{*/
	{
		$file = scandir($this->_html_dir);
		if ($file == false) {
			echo '[Server][NG] : Html files are not in SD card';
			return -1;
		}

		//echo '<pre>';  /*{{{*/
		//print_r($file);
		//echo '<pre>'; /*}}}*/

		$this->_html_files = array();
		for ($i=0; $i<sizeof($file);$i++)
		{
			$path = pathinfo($file[$i]);

			//echo '<pre>';  /*{{{*/
			//print_r ($path);
			//print_r ($path['extension']);
			//echo '<pre>';  /*}}}*/

			if (isset($path['extension'])) 
			{
				if ($path['extension'] == 'html' || $path['extension'] == 'htm'  ||
					$path['extension'] == 'HTML' || $path['extension'] == 'HTM' ) 
				{
					$this->_html_files[] = $file[$i];
					//echo '<pre>'; /*{{{*/
					//print_r($this->_html_files);
					//echo '<pre>'; /*}}}/
				}
			}//en-of-extension
		}
	} //end-of-function.getHtmlList  /*}}}*/

	/* ****************************************
	 *  For filemanage.php : show Existed Html
	 * ****************************************/
	public function showHtmlListTable() /*{{{*/
	{
		$this->getHtmlList();
		echo '<table border="0" cellspacing="0" cellpadding="0" width=370px class="form_table">';
		echo '<tr> <td class="name"> <strong class="green-text">HTML List</strong></td></tr>';
		echo '<tr>';
		echo '<td>';
		echo '<DIV id=div_html_radio_group>';
		for ($i=0; $i<sizeof($this->_html_files);$i++)
		{
			if ($i==0) {
				$checked = 'checked';
			} else {
				$checked = '';
			}
			$fileinfo = pathinfo($this->_html_files[$i]);
			echo '<input type="radio" name="html_radio" value="'; 
			echo $this->_html_files[$i];
			echo '"';
			echo $checked;
			echo '>';
			echo $this->_html_files[$i];
			echo '<br>';
		}
		echo '</DIV>';
		echo '</td>';
		echo '</tr>';

		//ROW2
		echo '<tr>';
		echo '<td>';
		echo '<input type="file" name="browse_image" value="Choose Image" id="browse_image"  /> 
			<input type="submit" name="upload_image" value="upload image" id="upload_image"    onClick="return lastname(\'browse_image\',\'jpg,png,bmp,gif\')"/> ';
		echo '</td>';
		echo '</tr>';

		//ROW3
		echo '<tr>';
		echo '<td>';
		echo '<input type="submit" name="delete_html" value="delete">';
		echo '</td>';
		echo '</tr>';

		echo '</table>';
	} /*}}}*/

	/* ****************************************
 	*   The list from /media/sd/movies
 	* *****************************************/
	public function getMovieList() /*{{{*/
	{
		$file = scandir($this->_movie_dir);
		$this->_movie_files = array();
		for ($i=0; $i<sizeof($file);$i++)
		{
			$path = pathinfo($file[$i]);
			//echo '<pre>';  /*{{{*/
			//print_r ($path);
			//print_r ($path['extension']);
			//echo '<pre>';  /*}}}*/

			if (isset($path['extension'])) 
			{
				if ($path['extension'] == 'avi' || 
					$path['extension'] == 'wmv'  ||
					$path['extension'] == 'flv' ||
					$path['extension'] == 'AVI' ||
					$path['extension'] == 'WMV' ||
					$path['extension'] == 'FLV' ||
					$path['extension'] == 'mp4' ||
					$path['extension'] == 'MP4' || 
					$path['extension'] == 'jpg' ||
					$path['extension'] == 'jpeg' || 
					$path['extension'] == 'png' || 
					$path['extension'] == 'gif' || 
					$path['extension'] == 'JPG' || 
					$path['extension'] == 'JPEG' || 
					$path['extension'] == 'GIF' || 
					$path['extension'] == 'PNG' ||
					$path['extension'] == 'bmp' ||
					$path['extension'] == 'BMP' ) 
				{
					$this->_movie_files[] = $file[$i];
					//echo '<pre>';
					//print_r($this->_movie_files);
					//echo '<pre>';
				}
			}//en-of-extension
		}
	} /*}}}*/

	/* ******************************************
	 *  For filemanage.php : show Existed movies
	 * ******************************************/
	public function showMovieListTable()  /*{{{*/
	{
		$this->getMovieList();
		//TODO
		//when the array is empty
		echo '<table border="0" cellspacing="0" cellpadding="0" width=370px class="form_table">';
		echo '<tr> <td class="name"> <strong class="green-text"> Movie List</strong></td></tr>';
		echo '<tr>';
		echo '<td>';
		for ($i=0; $i<sizeof($this->_movie_files);$i++)
		{
			if ($i==0) {
				$checked = 'checked';
			} else {
				$checked = '';
			}
			echo '<input type="radio" name="movie_radio" value="'; 
			echo $this->_movie_files[$i];
			echo '"';
			echo $checked;
			echo '>';
			echo $this->_movie_files[$i];
			echo '<br>';
		}
		echo '</td>';
		echo '</tr>';
		//ROW3
		echo '<tr>';
		echo '<td>';
		echo '<input type="submit" name="delete_movie" value="delete">';
		echo '</td>';
		echo '</tr>';
		echo '</table>';
	} /*}}}*/

	/*}}}*/

	// =====================================================
 	//  For routinelist.php
	// =====================================================
	private function readRoutineList() /*{{{*/
	{
		$routine_handle = fopen($this->_routine_path,"r");
		if (false == $routine_handle) {
			echo '[Warnging] No Playlist File';
			return;
		}	

		$routineList = array();
		while (!feof($routine_handle)) 
		{
			$readline = fgets($routine_handle);
			if (!empty($readline)) {
				$routineList[] = $readline;
			}
		}
		//print_r($routineList);
		$this->_routine_ini = $routineList;
		//var_dump($this->_routine_ini);
		//echo '<pre>';
		//print_r $this->_routine_ini;
		//echo  '</pre>';
	} /*}}}*/

	public function showRoutineListTable()  /*{{{*/
	{
		$playlist_path = '/usr/infomat/playlist';
		$playlist_handle = fopen($playlist_path, "r");
		if (!$playlist_handle) {
			echo 'Error No Playlist File';
			return;
		}	
		/*
	 	 * ____________________________________________
	 	 * | No |  Html |  Movie | E | Timeout | chk |
	 	 * --------------------------------------------
	  	 */
		echo '<table class="form_table" border="0" cellpadding="0" width="750px" id="idRoutineListTable">';
		echo '<tr><td style="width:3%" style="border-width:1px" align="left"><strong class="orange-text-regular">No</strong></td>';
		echo '<td style="width:25%" style="border-width:1px" align="center"><strong class="orange-text-regular">HTML FILE</strong></td>';
		echo '<td style="width:25%" style="border-width:1px" align="center"><strong class="orange-text-regular">Movie FILE</strong></td>';
		echo '<td style="width:5%" style="border-width:1px" align="center"><strong class="orange-text-regular"></strong></td>';
		echo '<td style="width:15%" style="border-width:1px" align="center"><strong class="orange-text-regular">Timeout(sec)</strong></td>';
		echo '<td style="width:5%" style="border-width:1px align="center"><strong class="orange-text-regular"></strong></td>';
		echo '</tr>';
		$i=0;
		while (!feof($playlist_handle)) 
		{
			$buffer = trim(fgets($playlist_handle));
			//var_dump($buffer);
			if (!empty($buffer))
			{
				if ($i%2==0) {
					$class_style = 'class="routineListCell"';
				} else { 
					$class_style = "";
				}
				$i++;
			
				list($sec1,$sec2,$sec3,$sec4)=explode(':',$buffer);

				echo '<tr>'; // --1--
				echo '<td style="border-width:1px" ';
				echo $class_style.'>'; // --1.1-- 
				//if (isset($sec1)) { echo $sec1; } else {echo '--';}
				echo $i;
				echo '</td>'; 

				echo '<td style="border-width:1px" ';  // --1.2-- //html
				echo $class_style.'>';
				if (isset($sec2)) { 
					if (empty($sec2))
						echo '----';
					else 
						echo $sec2; 
				} else {
					echo '----';
				}
				echo '</td>';

				echo '<td style="border-width:1px" '; // --1.3-- //movie
				echo $class_style.'>';
				if (isset($sec3)) { 
					if (empty($sec3))
						echo '--';
					else 
						echo $sec3; 
				} else {
					echo '--';
				}
				echo '</td>';

				echo '<td style="border-width:1px" ';  // --1.3.1 -- movie/Edit
				echo $class_style.'>';
				echo "<input type='button' value='E' onclick=\"javascript:location.href='editlist.php?id=$i';\"> </input>";
				echo '</td>';
				
				echo '<td style="border-width:1px;text-align:center" ';
				echo $class_style.'>';
				echo '<input type="text" size="2" maxlength="2" value="';
				if (isset($sec4)) { 
					if (empty($sec4)) {
						echo '10'; //default value
					}else {
						echo $sec4/1000; 
					}
				} else {
					echo '10';
				}
				echo '" />';
				echo '</td>';

				echo '<td style="align:center;" ';  // --1.4 --
				echo $class_style.'>';
				echo "<input type='checkbox' name='checkbox_$i' id='idCheckBox'></input>";
				echo '</td>';
				echo '</tr>'; 
			} 
		}
		echo '</table>'; //end-of-table
	} //end-of-showRoutineListTable() /*}}}*/

	public function deleteSpecifiedRecords($in_toDeleteRecord)  /*{{{*/
	{
		if (empty($in_toDeleteRecord)) {
			echo '[Waring] No records pass in';
			return;
		}

		$this->readRoutineList();
		if (empty($this->_routine_ini)) {
			echo "[waring] no records in playlist, check out your path: $this->_routine_path";
			return;
		}
		
		// delete the record
		foreach ($in_toDeleteRecord as $toDelindex) {
			unset($this->_routine_ini[$toDelindex]);
		}

		$newRoutineIni = array_values($this->_routine_ini);
		$this->_routine_ini = array();
		$this->_routine_ini = $newRoutineIni;
		//echo '<pre>';
		//print_r($this->_routine_ini);
		//echo '<pre>';
		//echo '<pre>';
		//print_r($newRoutineIni);
		//echo '<pre>';
	} /*}}}*/

	public function saveRoutineIni() /*{{{*/
	{
		if (false == rename ($this->_routine_path, $this->_routine_bak_path)) {
			echo "backup playlist error";
			return;
		}

		$handle = fopen ($this->_routine_path, "w");
		if (false!=$handle) {
			foreach ($this->_routine_ini as $newline) {
				if (!feof($handle)) {
					fwrite($handle, $newline);
				}
			}
		} else { 
			echo "write file error";
			return;
		}
	} /*}}}*/

	public function restoreRoutineList()  /*{{{*/
	{
		echo "todo: restore backup file";
	} /*}}}*/

	// ====================================================
  	//  For editlist.php 
	// ====================================================	
	public function setMode($in_currentIndex)  /*{{{*/
	{
		if ($in_currentIndex == -1 ) 
		{
			$this->_mode = -1; //add new 
		} else {
			$this->_mode = 0;  //edit 
		}
	} /*}}}*/

	public function showHtmlListTableForEditList($currentIndex) /*{{{*/
	{
		$this->getHtmlList();
		
		/*
		 * --> Add  --> Blank 
		 * --> Edit --> Show Selected Item
		 */
		if ($currentIndex == -1) { //Function for Add New
			$isBlank = "checked";
			$isChecked = "";
			$sec2 = "";
		} else {                   //Function for Edit Existed item
			$content = file($this->_routine_path);
			//echo "showHtmlListTableForEditList() <br>";
			//print_r($content);
			if(!empty($content)) { //when playlist is empty
				//print_r($content);
				if (isset($content[$currentIndex-1])) {
					$lineWithIndex = $content[$currentIndex-1];
					//echo $lineWithIndex;
					list($sec1,$sec2,$sec3,$sec4)=explode(':',$lineWithIndex);
					if($sec2 == "") {  // no html for the item 
						$isBlank = "checked";
						$isChecked = "";
					}
				} else {
					echo "Error: Pass in wrong Index : when playlist is empty, it should be pass in add command";
					return;
				}
			} else {
				echo "Error : Should Not Be Here - if playlist is empty, it should pass in add command";
				return;
			}
			//echo $sec2;
		} 

		echo '<table border="0" cellspacing="0" cellpadding="0" width="350px" class="form_table">';
		echo '<tr> <td class="name"> <strong class="green-text">HTML List</strong></td></tr>';
		echo '<tr>';
		echo '<td>';
		echo '<DIV id=div_html_radio_group>';
		for ($i=0; $i<sizeof($this->_html_files);$i++)
		{ 
			if ($sec2 =="") {
				$isBlank = "checked";
				$isChecked = "";
			} else {
				$isBlank = "";
				if ($sec2 == $this->_html_files[$i]) {
					//echo $sec2;
					//echo "<br>";
					//echo $this->_html_files[$i];
					//echo "<br >";
					$isChecked = "checked";	
					$isBlank ="";
				} else {
					$isChecked = "";	
				}
			}

			if ($i==0) {
				echo '<input type="radio" name="html_radio" value="val_blank" id="id_html_blank" ';
				echo $isBlank;	
				echo ">Blank</input>";
				echo '<br>';
			} 
			echo '<input type="radio" name="html_radio" value="'; 
			echo $this->_html_files[$i];
			echo '" ';
			echo $isChecked;
			echo '>';
			echo $this->_html_files[$i];
			echo '<br>';
		}
		echo '</DIV>';
		echo '</td>';
		echo '</tr>';

		echo '</table>';
	} /*}}}*/
	
	public function showMovieListTableForEditList($in_currentIndex)  /*{{{*/
	{
		$this->getMovieList();
		// retrieve the html with currentIndex 
		if ($in_currentIndex == -1) {
			//Process Add New
			$isBlank = "checked";
			$isChecked = "";
			$sec3 = "";
		} else {
			//Process Edit
			$content = file($this->_routine_path);
			if(!empty($content)) {
				$lineWithIndex = $content[$in_currentIndex-1];
				//echo $lineWithIndex; echo "<br>";
				list($sec1,$sec2,$sec3,$sec4)=explode(':',$lineWithIndex);
			} else {
				echo "Error: show Movie List Table for editlist, playlist is empty, this is edit function";
			}	
		}
		
		echo '<table border="0" cellspacing="0" cellpadding="0" width=370px class="form_table">';
		echo '<tr> <td class="name"> <strong class="green-text"> Movie List</strong></td></tr>';
		echo '<tr>';
		echo '<td>';
		for ($i=0; $i<sizeof($this->_movie_files);$i++)
		{
			if ($sec3 == "") 
			{ 
				$isBlank = "checked";
				$isChecked = "";
			}else{
				$isBlank = "";
				if ($sec3 == $this->_movie_files[$i]) {
					//echo $sec3;
					//echo "<br>";
					//echo $this->_movie_files[$i];
					//echo "<br >";
					$isChecked = "checked";	
				} else {
					$isChecked = "";	
				}
			}

			if ($i==0) {
				echo '<input type="radio" name="movie_radio" value="val_blank" id="id_movie_blank" ';
				echo  $isBlank;	
				echo '>Blank</input>';
				echo '<br>';
			} 

			echo '<input type="radio" name="movie_radio" value="'; 
			echo $this->_movie_files[$i]."\" ".$isChecked;
			echo '>';
			echo $this->_movie_files[$i];
			echo '</input>';
			echo '<br>';
		}
		echo '</td>';
		echo '</tr>';
		echo '</table>';
	} /*}}}*/

	public function setCurrentIndex($in_currentIndex) /*{{{*/
	{
		$this->_currentIndex = $in_currentIndex;
	} /*}}}*/

	public function saveRoutineList($html,$movie)  /*{{{*/
	{
		//echo "[enter] saveRoutineList <br>";
		$content = file($this->_routine_path);
		$length = count($content);
		if ($this->_mode == -1) { //add new
			$newItem = ($length+1).$html.":"."$movie".":"."10000";
			array_push($content,$newItem);
		  	print_r($content);
		}
		return ;

		/*
		if (!empty($content)) {
			$lineWithIndex = $content[$this->_currentIndex-1];
			//echo $lineWithIndex; echo "<br>";
			list($sec1,$sec2,$sec3,$sec4)=explode(':',$lineWithIndex);
			$sec2 = $html;
			$sec3 = $movie;
			$content[$this->_currentIndex-1] = $sec1.":".$sec2.":".$sec3.":".$sec4;
		}else {
			$content[0] = "1:".$html.":"."$movie".":"."10000";
		}
		//echo $content[$sec1-1];
		//echo "<pre>"; 
		//print_r($content);
		//echo "<pre>";
		unlink($this->_routine_path);
		$fd = fopen($this->_routine_path, 'w');
		foreach ($content as $line) {
			fwrite($fd, $line);
		}
		fclose($fd);
		 */
	} /*}}}*/
}  //end-of-class

//$xlist = new XPlayList ();
//$xlist->showMovieListTable();
//$xlist->showRoutineListTable();
//$xlist->readPlayList();
//$arr = array ();
//$arr[0] = 1;
//$arr[1]=2;
//$xlist->deleteSpecifiedRecords($arr);
//$xlist->saveRoutine();
//$xlist->showMovieListTableForEditList(2);
?>
